package com.heima.stroke.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.heima.commons.domin.bo.RoutePlanResultBO;
import com.heima.commons.domin.bo.TextValue;
import com.heima.commons.utils.HttpClientUtils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class BaiduMapClient {
    @Value("${baidu.map.api}")
    private String api;
    @Value("${baidu.map.ak}")
    private String ak;

    private final static Logger logger = LoggerFactory.getLogger(BaiduMapClient.class);

    //TODO:任务3.2-调百度路径计算两点间的距离，和预估抵达时长
    public RoutePlanResultBO pathPlanning(String origins, String destinations) {
        String tactics="13";
        String url = "https://api.map.baidu.com/routematrix/v2/driving?"
                + "origins=" + origins
                + "&destinations=" + destinations
                + "&tactics=13" // 根据您的需求设置路径计算策略
                + "&ak=" + ak;
        System.out.println("生成的请求 URL：" + url);
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();


        TextValue distanceTextValue = new TextValue();
        TextValue durationTextValue = new TextValue();

        try {
            Response response = client.newCall(request).execute();
            String responsBody=response.body().string();
            if (response.isSuccessful()) {
                System.out.println("请求成功");
                System.out.println("响应数据：");
                System.out.println(responsBody);
                // 这里可以根据返回的数据进行处理

                // 使用 Gson 解析 JSON
                Gson gson = new Gson();
                JsonObject jsonObject = gson.fromJson(responsBody, JsonObject.class);

                JsonObject result = jsonObject.getAsJsonArray("result").get(0).getAsJsonObject();

                // Parsing distance
                JsonObject distance = result.getAsJsonObject("distance");
                String distanceText = distance.get("text").getAsString();
                int distanceVal = distance.get("value").getAsInt();

                // Parsing duration
                JsonObject duration = result.getAsJsonObject("duration");
                String durationText = duration.get("text").getAsString();
                int durationVal = duration.get("value").getAsInt();

                // Creating TextValue instances

                distanceTextValue.setText(distanceText);
                distanceTextValue.setValue(distanceVal);


                durationTextValue.setText(durationText);
                durationTextValue.setValue(durationVal);




            } else {
                System.out.println("请求失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        RoutePlanResultBO routePlanResultBO = new RoutePlanResultBO();
        routePlanResultBO.setDistance(distanceTextValue);
        routePlanResultBO.setDuration(durationTextValue);


        return routePlanResultBO;

    }

}
